package evil.wolf.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

import evil.wolf.excute.query.ExcuteQuery;
import evil.wolf.objects.ObjectCTNhapXuat;
import evil.wolf.objects.ObjectNhapXuat;
import evil.wolf.panels.ListCTNhapXuatUI;
import evil.wolf.panels.ListNhapUI;


/**
 * A panel with the title and the text of a book.
 * 
 * @author Heidi Rakels.
 * 
 */

@SuppressWarnings("serial")
public class UICTNhapXuat extends JDialog{
	
	Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
	
	// Static fields.
	@SuppressWarnings("unused")
	private static final String NEWLINE = System.getProperty("line.separator");
	
	public JTextField txtsoct ;
	public JTextField txtmak ;
	public JTextField txtmah;
	public JTextField txtsl;
	public JTextField txtdg;
	public JTextField txttg;
	
	public JButton buttonAction;
	public JButton btnCancel;
	
	ListNhapUI listNhapXuatUI;
	ListCTNhapXuatUI listCTNhapXuatUI;
	ObjectCTNhapXuat objectCTNhapXuat;
	ObjectNhapXuat objectNhapXuat;
	MainUI mainUI;
	public String oldValue;
	

	public UICTNhapXuat(MainUI mainUI, ListNhapUI listNhapXuatUI, ListCTNhapXuatUI listCTNhapXuatUI, ObjectCTNhapXuat objectCTNhapXuat, ObjectNhapXuat objectNhapXuat) {
		super();
		this.listNhapXuatUI = listNhapXuatUI; 
		this.mainUI = mainUI;
		this.listCTNhapXuatUI = listCTNhapXuatUI;
		this.objectCTNhapXuat = objectCTNhapXuat;
		this.objectNhapXuat = objectNhapXuat;
		add(streamData());
		
		if(listCTNhapXuatUI.getAction() == null) 
			listCTNhapXuatUI.setAction(evil.wolf.action.Action.ACTION_INSERT);
		
		this.setAlwaysOnTop(true);
		setSize(360, 250);
		this.setTitle("Chi tiết nhập xuất");
		setResizable(false);
		setLocation((dim.width - getWidth())/2, (dim.height - getHeight())/2);

	}
	
	public JPanel streamData()
	{
		JPanel mainPanel = new JPanel();
		
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
		mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 10));
		
		JPanel helppanel1 = new JPanel(new BorderLayout());
		
		JPanel helppanel2 = new JPanel(new FlowLayout(FlowLayout.RIGHT, 0, 0));
		helppanel2.setPreferredSize(new Dimension(25, 110));
		
		JPanel labelpanel = new JPanel(new GridLayout(7, 1, 0, 0));
		labelpanel.setPreferredSize(new Dimension(70, 180));
		
		JLabel stremty = new JLabel(" ");
		labelpanel.add(stremty, Component.RIGHT_ALIGNMENT);
		
		JLabel strmah = new JLabel("Số chứng từ");
		labelpanel.add(strmah, Component.RIGHT_ALIGNMENT);
		
		JLabel strtenh = new JLabel("Mã kho");
		labelpanel.add(strtenh, Component.RIGHT_ALIGNMENT);
		
		JLabel strmanh = new JLabel("Mã hàng");
		labelpanel.add(strmanh, Component.RIGHT_ALIGNMENT);
				
		JLabel strdvt = new JLabel("Đơn giá");
		labelpanel.add(strdvt, Component.RIGHT_ALIGNMENT);
		
		JLabel strsl = new JLabel("Số lượng");
		labelpanel.add(strsl, Component.RIGHT_ALIGNMENT);
		
		JLabel strdd = new JLabel("Tổng giá");
		labelpanel.add(strdd, Component.RIGHT_ALIGNMENT);
		
		helppanel1.add(labelpanel, BorderLayout.CENTER);
		helppanel1.add(Box.createRigidArea(new Dimension(10, 10)), BorderLayout.WEST);
		helppanel1.add(helppanel2, BorderLayout.SOUTH);
		JPanel comm1 = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
		comm1.add(helppanel1);
		
		JPanel textpanel = new JPanel( new FlowLayout(FlowLayout.LEFT, 5, 5) );
		textpanel.setPreferredSize(new Dimension(260, 245)); 
		txtsoct = new JTextField(10);
		if (objectNhapXuat != null)
			txtsoct.setText(objectNhapXuat.getsoct());
		txtsoct.setEditable(false);
		textpanel.add(txtsoct);
		
		txtmak = new JTextField(31);
		textpanel.add(txtmak);

		JPanel tenh_mah_panel = new JPanel( new FlowLayout(FlowLayout.LEFT, 0, 5) );
		tenh_mah_panel.setPreferredSize(new Dimension(98, 73));
		txtmah = new JTextField(10);
		tenh_mah_panel.add(txtmah);
		tenh_mah_panel.add(Box.createRigidArea(new Dimension(0, 10)));
		txtdg = new JTextField(10);
		tenh_mah_panel.add(txtdg);
		txtsl = new JTextField(5);
		tenh_mah_panel.add(txtsl);
		textpanel.add(tenh_mah_panel);
		tenh_mah_panel.setBorder(BorderFactory.createEmptyBorder(-5, 0, 0, 0));
		
		JPanel dd_panel = new JPanel( new FlowLayout(FlowLayout.LEFT, 0, 0) );
		txttg = new JTextField(20);
		JScrollPane scroll = new  JScrollPane(txttg, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		dd_panel.add(scroll);
		textpanel.add(dd_panel);
		
		comm1.add(textpanel);
		
		mainPanel.add(comm1);
		
		JPanel buttonpanel = new JPanel(new GridLayout(1, 4 , 5 , 5) );
  	
		buttonpanel.add(Box.createRigidArea(new Dimension(0, 0)));
		buttonpanel.add(Box.createRigidArea(new Dimension(0, 0)));
		buttonAction = new JButton("Ghi");
		oldValue = txtsoct.getText().toString().trim();
		buttonAction.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				if(listCTNhapXuatUI.getAction().trim() == evil.wolf.action.Action.ACTION_INSERT){
					if (checkInDataExist()) {
						ObjectCTNhapXuat newObjectCTNhapXuat = new ObjectCTNhapXuat();
						
						newObjectCTNhapXuat .setsoct(objectNhapXuat.getsoct());
						newObjectCTNhapXuat .setmak(txtmak.getText().toString().trim());
						newObjectCTNhapXuat .setmah(txtmah.getText().toString().trim());
						newObjectCTNhapXuat .setdg(Integer.parseInt(txtdg.getText()));
						newObjectCTNhapXuat .setsl(Integer.parseInt(txtsl.getText()));
						txttg.setText(String.valueOf((Integer.parseInt(txtdg.getText()) * Integer.parseInt(txtsl.getText()))));
						newObjectCTNhapXuat .settg(Integer.parseInt(txttg.getText()));
						ExcuteQuery doAction = new ExcuteQuery();
						
						if(doAction.insertCTNhapXuat(newObjectCTNhapXuat)) {
						
							try{
								listCTNhapXuatUI.arrayLaterials.add(newObjectCTNhapXuat);
								listCTNhapXuatUI.dataTable.revalidate();
								setVisible(false);
								
//								Refresher ref = new Refresher(mainUI, newObjectCTNhapXuat, objectNhapXuat.getloaict());
//								ref.refresh(mainUI, newObjectCTNhapXuat.getmah(), newObjectCTNhapXuat.getsl());
							}
							catch(Exception ex){
								JOptionPane.showMessageDialog(null,	ex.toString(), "ERROR", JOptionPane.INFORMATION_MESSAGE);
								ex.printStackTrace();
							}
							
						}
					}
				}

//============================================================================================================
				if(listCTNhapXuatUI.getAction().trim() == evil.wolf.action.Action.ACTION_UPDATE) {
					
					ObjectCTNhapXuat mObjectCTNhapXuat = new ObjectCTNhapXuat();
					int row = listCTNhapXuatUI.getSelectedRow();
					if (checkInDataExist()) {
						
						mObjectCTNhapXuat .setsoct(txtsoct.getText().toString().trim());
						mObjectCTNhapXuat .setmak(txtmak.getText().toString().trim());
						mObjectCTNhapXuat .setmah(txtmah.getText().toString().trim());
						mObjectCTNhapXuat .setdg(Integer.parseInt(txtdg.getText()));						
						mObjectCTNhapXuat .setsl(Integer.parseInt(txtsl.getText()));
						txttg.setText(String.valueOf((Integer.parseInt(txtdg.getText())*Integer.parseInt(txtsl.getText()))));
						mObjectCTNhapXuat .settg(Integer.parseInt(txttg.getText()));
						
						ExcuteQuery doAction = new ExcuteQuery();
						if(doAction.updateCTNhapXuat(mObjectCTNhapXuat, objectCTNhapXuat.getsoct(), objectCTNhapXuat.getmak(), objectCTNhapXuat.getmah())) {
							
							try{
								listCTNhapXuatUI.arrayLaterials.remove(row);
								listCTNhapXuatUI.arrayLaterials.add(row, mObjectCTNhapXuat );
								listCTNhapXuatUI.dataTable.repaint();
								setVisible(false);
							}
							catch(Exception ex){
								JOptionPane.showMessageDialog(null,	ex.toString(), "ERROR", JOptionPane.INFORMATION_MESSAGE);
								ex.printStackTrace();
							}
						}
						
					}
				}
			}
		});
		
		buttonpanel.add(buttonAction, Component.RIGHT_ALIGNMENT) ;
		
		btnCancel = new JButton("Hủy");
		final UICTNhapXuat themhh;
		themhh = this;
		btnCancel.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				themhh.setVisible(false);
				if(listCTNhapXuatUI.getAction() == evil.wolf.action.Action.ACTION_INSERT) {
					ExcuteQuery exc = new ExcuteQuery();
					exc.deleteNhap(objectNhapXuat.getsoct(), objectNhapXuat.getmakh(), objectNhapXuat.getloaict());
				}
				
			}
		});
		buttonpanel.add(btnCancel, Component.RIGHT_ALIGNMENT) ;
		mainPanel.add(buttonpanel);
		
        return mainPanel;
	}

	public void setTextUI(ObjectCTNhapXuat objectCTNhapXuat){
		txtsoct.setText(objectCTNhapXuat.getsoct());
		txtmak.setText(objectCTNhapXuat.getmak());
		txtmah.setText(objectCTNhapXuat.getmah());
		txtdg.setText(String.valueOf(objectCTNhapXuat.getdg()));
		txttg.setText(String.valueOf(objectCTNhapXuat.gettg()));
		txtsl.setText(String.valueOf(objectCTNhapXuat.getsl()));
	}
	private boolean checkInDataExist() {
		if((txtsoct.getText().trim() == null) || (txtmak.getText().trim() == null) || (txtmah.getText().trim() == null) ||  (txtdg.getText().trim() == null)  || (txttg.getText().trim() == null))
			return false;
		else
			return true;
	}
	/**
	 * Adds the styles to a styled document.
	 * 
	 * @param doc		The styled document.
	 * @param picture	A picture used in the document.
	 */
	protected void addStylesToDocument(StyledDocument doc, Icon picture) {

        Style def = StyleContext.getDefaultStyleContext().
                        getStyle(StyleContext.DEFAULT_STYLE);

        Style regular = doc.addStyle("regular", def);
        StyleConstants.setFontFamily(def, "SansSerif");

        Style style = doc.addStyle("italic", regular);
        StyleConstants.setItalic(style, true);

        style = doc.addStyle("bold", regular);
        StyleConstants.setBold(style, true);

        style = doc.addStyle("small", regular);
        StyleConstants.setFontSize(style, 10);

        style = doc.addStyle("large", regular);
        StyleConstants.setFontSize(style, 16);
        StyleConstants.setBold(style, true);
        
        style = doc.addStyle("icon", regular);
        StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);
        
        if (picture != null) {
            StyleConstants.setIcon(style, picture);
        }
    }
	
}
